import java.util.Arrays;
public class MyArrayList {//区别标准库
    private  Object[] elements;
    private static final int DEFAULT_CAPACITY = 10;//初始容量为10
    private int size;
    public MyArrayList() {
        elements = new Object[DEFAULT_CAPACITY];
        size = 0;
    }//构造函数
    public void add(Object element) {//传入的元素类型为Object
        if (size == elements.length) {
            resize();
        }//如果数组已满，调用resize方法扩容
        elements[size++] = element;
    }//添加元素
    public void resize(){
        int newCapacity=elements.length*2;
        //elements=Arrays.copyOf(elements,newCapacity);可以调用array扩容
        Object[]   newElements=new Object[newCapacity];//new一个新数组
        for(int i=0;i<elements.length;i++){
            newElements[i]=elements[i];//将原数组的元素复制到新数组中
        }
        elements=newElements;//更新数组
    }
    public Object get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }//检查索引是否越界
        return elements[index];
    }//获取元素
    public static void main(String[] args) {
        MyArrayList list = new MyArrayList();
        list.add("Hello");
        list.add("World");
        System.out.println(list.get(0)); // 输出: Hello
        System.out.println(list.get(1)); // 输出: World
    }//测试代码


    
}
